// CPage9_RAnw.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CPage9_RAnw.h"
#include "CToggleButton.h"
#include "Charplandoc.h"


#define LIST_CTRLS_ID 5000

// CPage9_RAnw-Dialogfeld



IMPLEMENT_DYNAMIC(CPage9_RAnw, CAPage)
CPage9_RAnw::CPage9_RAnw(CWnd* pParent /*=NULL*/)
	: CAPage(CPage9_RAnw::IDD, pParent)
{
}


CPage9_RAnw::~CPage9_RAnw()
{
	DeleteList();
}

void CPage9_RAnw::DoDataExchange(CDataExchange* pDX)
{
	CAPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, mwnd_liste);
	DDX_Control(pDX, IDC_EDIT3, mwnd_description);
	DDX_Control(pDX, IDC_EDIT1, mwnd_pointsrest);
	DDX_Control(pDX, IDC_EDIT2, mwnd_pointsused);DDX_Control(pDX, IDC_CHECK1, mwnd_checkpoints);
	DDX_Control(pDX, IDC_EDIT4, mwnd_rr_needed);
	DDX_Control(pDX, IDC_EDIT5, mwnd_rps_needed);
}


BEGIN_MESSAGE_MAP(CPage9_RAnw, CAPage)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, OnNMCustomdrawList1)
	ON_BN_CLICKED(IDC_CHECK1, OnBnClickedCheck1)
	ON_NOTIFY(HDN_BEGINTRACK, IDC_LIST1, OnHdnBegintrackList1)
	ON_NOTIFY(HDN_ENDTRACK, IDC_LIST1, OnHdnEndtrackList1)
	ON_NOTIFY(LVN_CELL_POS_CHANGED, IDC_LIST1, OnHdnEndtrackList1)
    ON_BN_DOUBLECLICKED(IDC_RA_RESET, &CPage9_RAnw::OnBnDoubleclickedRaReset)
END_MESSAGE_MAP()


// CPage9_RAnw-Meldungshandler

BOOL CPage9_RAnw::OnInitDialog()
{
	CAPage::OnInitDialog();

	CString temp;

	temp.LoadString(IDS_PAGE_RANF_TITLE1);	mwnd_liste.InsertColumn(0,temp);
	temp.LoadString(IDS_PAGE_RANF_TITLE2);	mwnd_liste.InsertColumn(1,temp);
	temp.LoadString(IDS_PAGE_RANF_TITLE3);	mwnd_liste.InsertColumn(2,temp);
	temp.LoadString(IDS_PAGE_RANF_TITLE4);	mwnd_liste.InsertColumn(3,temp);
	temp.LoadString(IDS_PAGE_RANF_TITLE5);	mwnd_liste.InsertColumn(4,temp);
	mwnd_liste.SetExtendedStyle(LVS_EX_FULLROWSELECT );

	if (GetDocument()->GetOption(_T("CheckRAPoints")))
		mwnd_checkpoints.SetCheck(true);
	OnBnClickedCheck1();

	OnClassChanged();

    // Resizer
    static const CResizer::CBorderInfo s_bi[] = {
        {IDC_LIST1, {CResizer::eFixed, IDC_MAIN, CResizer::eLeft},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eBottom}},
        {IDC_EDIT1, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop}},
        {IDC_STATIC5, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop}},
        {IDC_EDIT2, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop}},
        {IDC_STATIC6, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop}},
        {IDC_RA_RESET, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop}},
        {IDC_CHECK1, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop}},
        {IDC_STATIC4, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop}},
        {IDC_STATIC1, {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop}},
        {IDC_EDIT4, {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop}},
        {IDC_STATIC2, {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop}},
        {IDC_EDIT5, {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eLeft},
                    {CResizer::eFixed, IDC_STATIC4, CResizer::eTop}},
        {IDC_EDIT3, {CResizer::eFixed, IDC_LIST1, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eTop},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eRight},
                    {CResizer::eFixed, IDC_MAIN, CResizer::eBottom}},
    };
    m_resizer.Init(m_hWnd, NULL, s_bi, sizeof(s_bi)/sizeof(s_bi[0]));

	return TRUE;
}

void CPage9_RAnw::OnBnClickedCheck1()
{
	if (mwnd_checkpoints.GetCheck())
	{
		GetDocument()->SetOption(_T("CheckRAPoints"),1);
		mwnd_rps_needed.ShowWindow(SW_SHOW);
		mwnd_rr_needed.ShowWindow(SW_SHOW);
		GetDlgItem(IDC_STATIC1)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_STATIC2)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_STATIC4)->ShowWindow(SW_SHOW);
	}
	else
	{
		GetDocument()->SetOption(_T("CheckRAPoints"),0);
		mwnd_rps_needed.ShowWindow(SW_HIDE);
		mwnd_rr_needed.ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATIC1)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATIC2)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATIC4)->ShowWindow(SW_HIDE);
	}
}

void CPage9_RAnw::OnClassChanged()
{
	mn_last_description = -1;
	mwnd_description.SetWindowText(_T(""));
	FillList();
    TRACE("Recalc points: OnClassChanged");
	UpdatePoints();
}

void CPage9_RAnw::OnLevelChanged()
{
    TRACE("Recalc points: OnLevelChanged");
	UpdatePoints();
}


void CPage9_RAnw::DeleteList()
{
	// delete all controll pointers stored in the list
	for (int t=0;t<mdb_lines.GetCount();t++)
	{
		for (int b=0;b<number_of_ra_ranks;b++)
			if (mdb_lines[t].mwnd_but[b])
			{
				mdb_lines[t].mwnd_but[b]->DestroyWindow();
				delete (mdb_lines[t].mwnd_but[b]);
			}
	}
	mdb_lines.RemoveAll();
}



void	CPage9_RAnw::FillList()
{
	DeleteList();


	// get data
    IntArray a_raids;
	gdbinterface.GetListOfRANF(a_raids);

	for (int t=0;t<a_raids.GetCount();t++)
	{
		s_list_data new_item;

		gdbinterface.SelectRANF(a_raids[t]);
        new_item.ra_id = (enum_RA_id) a_raids[t];
		new_item.name = gdbinterface.GetRANFName();
		gdbinterface.GetRAInfoNF(new_item.shortname,new_item.reuse,new_item.ratype, new_item.type);

		new_item.currentlevel = GetDocument()->GetRRNFLevel(new_item.ra_id);
		gdbinterface.GetRALevelsNF(new_item.levels);

        int count=0;
        for (;count<number_of_ra_ranks;++count)
            if (new_item.levels[count].empty()) break;
        new_item.count_of_levels = count;

		mdb_lines.Add(new_item);
	}


	// sort
	for (int t=0;t<mdb_lines.GetCount()-1;t++)
		for (int t2=t+1;t2<mdb_lines.GetCount();t2++)
		{
			int dif = mdb_lines[t].ratype-mdb_lines[t2].ratype;
			if (dif >0 || (dif ==0 && mdb_lines[t].name>mdb_lines[t2].name))
			{
				s_list_data temp;
				temp = mdb_lines[t];
				mdb_lines[t] = mdb_lines[t2];
				mdb_lines[t2] = temp;
			}
		}


	// Insert seperator
	//if (mn_sort == 2)
	{
		int line=0;
		while (line<mdb_lines.GetCount())
		{
			if (line==0 || mdb_lines[line].ratype != mdb_lines[line-1].ratype)
			{
				if (line<mdb_lines.GetCount())
				{
					s_list_data new_item;

                    new_item.ra_id = (enum_RA_id)-1;
                    for (int t=0;t<number_of_ra_ranks;++t)
					    new_item.levels[t].clear();

					new_item.type = 100+mdb_lines[line].ratype;

					switch (mdb_lines[line].ratype)
					{
						case 0:		new_item.name = LoadResourceString(IDS_RA_TYPE_0);
									new_item.levels[0] = "1";
									new_item.levels[1] = "1";
									new_item.levels[2] = "2";
									new_item.levels[3] = "3";
									new_item.levels[4] = "3";
									new_item.levels[5] = "5";
									new_item.levels[6] = "5";
									new_item.levels[7] = "7";
									new_item.levels[8] = "7";
                                    new_item.count_of_levels=9;
									break;
						case 1:		new_item.name = LoadResourceString(IDS_RA_TYPE_1);
									new_item.levels[0] = "5";
									new_item.levels[1] = "5";
									new_item.levels[2] = "5";
									new_item.levels[3] = "7";
									new_item.levels[4] = "8";
                                    new_item.count_of_levels=5;
									break;
						case 2:		new_item.name = LoadResourceString(IDS_RA_TYPE_2);
									break;
					}
                    
					new_item.currentlevel = 0;
					new_item.reuse.clear();

					mdb_lines.InsertAt(line,new_item);
				}
				line++;
			}

			line++;
		}
	}




	// Fill window
	mwnd_liste.SetRedraw(FALSE);
	mwnd_liste.DeleteAllItems();
	for (int t=0;t<mdb_lines.GetCount();t++)
	{
		mdb_lines[t].itemnr = mwnd_liste.InsertItem(mwnd_liste.GetItemCount(),mdb_lines[t].name.c_str());

		mwnd_liste.SetItem(mdb_lines[t].itemnr,1,LVIF_TEXT,mdb_lines[t].shortname.c_str(),0,0,0,NULL);
		mwnd_liste.SetItem(mdb_lines[t].itemnr,3,LVIF_TEXT,mdb_lines[t].reuse.c_str(),0,0,0,NULL);

		mwnd_liste.SetItemData(mdb_lines[t].itemnr,t);

		// Active passive
		switch (mdb_lines[t].type)
		{
			case 1:
				mwnd_liste.SetItem(mdb_lines[t].itemnr,2,LVIF_TEXT,_T("Passive"),0,0,0,NULL);
				break;
			case 2:
				mwnd_liste.SetItem(mdb_lines[t].itemnr,2,LVIF_TEXT,_T("Active"),0,0,0,NULL);
				break;

            case 100: case 101: case 102: // header lines
                break;
			default:
				ASSERT(false);
		}
	}



	mwnd_liste.SetColumnWidth(0,LVSCW_AUTOSIZE);
	mwnd_liste.SetColumnWidth(1,LVSCW_AUTOSIZE);
	mwnd_liste.SetColumnWidth(2,LVSCW_AUTOSIZE);
	mwnd_liste.SetColumnWidth(3,LVSCW_AUTOSIZE);
	mwnd_liste.SetColumnWidth(4,350);


	// Create BUTTONS Controls
	for (int t=0;t<mdb_lines.GetCount();t++)
		{
			CRect r;
			GetCellRect(mdb_lines[t].itemnr,4,LVIR_BOUNDS,r);
			
			for (int b=0;b<number_of_ra_ranks;b++)
			{
				mdb_lines[t].mwnd_but[b]=NULL;
				if (!mdb_lines[t].levels[b].empty() && mdb_lines[t].type<100)
				{
                    int n = mdb_lines[t].count_of_levels;
					CRect br = r;
					int w = r.Width() / n;
					br.left = r.left+w*b;
					br.right = r.left+w*b+w;
				
					mdb_lines[t].mwnd_but[b] = new CToggleButton;

					mdb_lines[t].mwnd_but[b]->Create( mdb_lines[t].levels[b].c_str(),
							WS_CHILD | WS_VISIBLE | WS_TABSTOP,
							br,&mwnd_liste,LIST_CTRLS_ID+t*number_of_ra_ranks+b);

					mdb_lines[t].mwnd_but[b]->SetFont(mwnd_liste.GetFont());
					mdb_lines[t].mwnd_but[b]->SetMessageTarget(this);


					if (mdb_lines[t].currentlevel >= b+1)
						mdb_lines[t].mwnd_but[b]->SetPushed(true);
				}
			}
		}


	mwnd_liste.SetRedraw(TRUE);
	mwnd_liste.Invalidate();
}


void CPage9_RAnw::OnHdnBegintrackList1(NMHDR *, LRESULT *pResult)
{
	for (int t=0;t<mdb_lines.GetCount();t++)
		for (int b=0;b<number_of_ra_ranks;b++)
		if (mdb_lines[t].mwnd_but[b] != NULL)
		{
			mdb_lines[t].mwnd_but[b]->SetRedraw(FALSE);
		}

	*pResult = 0;
}

void CPage9_RAnw::OnHdnEndtrackList1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

    // since cells are still on old pos we've to calculate the movement
    int begin_dis=0;
    int end_dis=0;

    if (phdr->iItem<4)
    {
        CRect r;
        GetCellRect(0,phdr->iItem,LVIR_BOUNDS,r);
        begin_dis = phdr->pitem->cxy - r.Width();
    }
    
    if (phdr->iItem==4)
    {
        CRect r;
        GetCellRect(0,4,LVIR_BOUNDS,r);
        end_dis = phdr->pitem->cxy - r.Width();
    }

    UpdateControlPositions(begin_dis,end_dis);

	*pResult = 0;
}



void	CPage9_RAnw::UpdateControlPositions(int begin_dis, int end_dis)
{
	CRect client;
	CRect header;
	mwnd_liste.GetClientRect(client);
	mwnd_liste.GetHeaderCtrl()->GetWindowRect(header);
	client.top+=header.Height()+2;

	for (int t=0;t<mdb_lines.GetCount();t++)
	{
		CRect r;
		GetCellRect(mdb_lines[t].itemnr,4,LVIR_BOUNDS,r);
        r.right += end_dis;
        r.left += begin_dis;

		if ((r&client).IsRectEmpty())
		{
			for (int b=0;b<number_of_ra_ranks;b++)
				if (mdb_lines[t].mwnd_but[b] != NULL)
					mdb_lines[t].mwnd_but[b]->ShowWindow(SW_HIDE);
		}
		else 
		{
			for (int b=0;b<number_of_ra_ranks;b++)
				if (mdb_lines[t].mwnd_but[b] != NULL)
				{
					mdb_lines[t].mwnd_but[b]->SetRedraw(TRUE);
					CRect br = r;
                    int n = mdb_lines[t].count_of_levels;
					int w = (r.Width())/n;
					br.left = r.left+w*b;
					br.right = r.left+w*b+w;
					mdb_lines[t].mwnd_but[b]->ShowWindow(SW_SHOW);
					mdb_lines[t].mwnd_but[b]->MoveWindow(br,true);
					mdb_lines[t].mwnd_but[b]->SetRedraw(true);
					mdb_lines[t].mwnd_but[b]->Invalidate(FALSE);
				}
		}
	}
}



/**
 Calc rectangle of a specific cell in the listview
**/
BOOL CPage9_RAnw::GetCellRect(int iRow, int iCol, int nArea, CRect &rect)
{
	 if(iCol)
		return mwnd_liste.GetSubItemRect(iRow, iCol, nArea, rect);


	iCol = 1;
	CRect rCol1;
	if(!mwnd_liste.GetSubItemRect(iRow, iCol, nArea, rCol1))
		return FALSE;

	if(!mwnd_liste.GetItemRect(iRow, rect, nArea))
		return FALSE;

	rect.right = rCol1.left;

	return TRUE;
}

void	CPage9_RAnw::OnRRChanged()
{
    TRACE("Recalc points: OnRRChanged");
	UpdatePoints();
}

void	CPage9_RAnw::UpdatePoints()
{
	// calc used
	int p = 0;

    TRACE("RA:UpdatePoints->");
	for (int t=0;t<mdb_lines.GetCount();t++)
	{
		int b;
		for (b=0;b<number_of_ra_ranks;b++)
			if (mdb_lines[t].mwnd_but[b]!=NULL)
            {
                ASSERT(mdb_lines[t].ra_id != -1);
				if (mdb_lines[t].mwnd_but[b]->IsPushed() && mdb_lines[t].ra_id != -1)
				{
					gdbinterface.SelectRANF(mdb_lines[t].ra_id);
                    int costs = gdbinterface.GetRANFCost(b);
					p += gdbinterface.GetRANFCost(b);

                    TRACE(" %s_%i : %i",gdbinterface.GetRANFName(),b+1,costs);
				}
            }
	}
    TRACE("= Total: %i",p);

    int free_points = GetDocument()->GetRRPoints() - p;
    TRACE("free_points= %i",free_points);
    ShowUsedRRPoints(p,free_points);

	CheckPossibility(free_points);
}


void CPage9_RAnw::ShowUsedRRPoints(int used_points, int free_points)
{
	// Output
	CString temp;
	temp.Format(_T("%i"), used_points);
	mwnd_pointsused.SetWindowText(temp);

	temp.Format(_T("%i"),free_points);
	mwnd_pointsrest.SetWindowText(temp);

	// RR / RPs
	if (GetDocument()->GetOption(_T("CoopMode")))
	{
		used_points -= max( (int)(GetDocument()->GetLevel())-20,0);
	}

	temp.Format(_T("R%iL%i"),1+ used_points/10, used_points%10);
	if (used_points >120)	temp = _T("impossible");
	mwnd_rr_needed.SetWindowText(temp);

	temp = GetDocument()->FormatRps(max(used_points,0)) + _T(" RPs");
	mwnd_rps_needed.SetWindowText(temp);
}


BOOL CPage9_RAnw::OnCommand(WPARAM wParam, LPARAM lParam)
{

	if (HIWORD(wParam) == BN_CLICKED)
	{
		int line = (LOWORD(wParam)-LIST_CTRLS_ID)/number_of_ra_ranks;
		int sub =  (LOWORD(wParam)-LIST_CTRLS_ID)%number_of_ra_ranks;
		if (line >=0 && line <mdb_lines.GetCount())
		{

			ASSERT(mdb_lines[line].mwnd_but[sub]!=NULL);


			if (mdb_lines[line].currentlevel != sub+1)//mdb_lines[line].mwnd_but[sub]->IsPushed())
			{

				if (!mwnd_checkpoints.GetCheck())
				{
					if (!mdb_lines[line].mwnd_but[sub]->IsHighLit())
					{
						mdb_lines[line].mwnd_but[sub]->SetPushed(false);
						return TRUE;
					}
				}


				mdb_lines[line].currentlevel = sub+1;
				GetDocument()->SetRRNFLevel(mdb_lines[line].ra_id,sub+1);
				for (int t=0;t<number_of_ra_ranks;t++)
					if (mdb_lines[line].mwnd_but[t]!=NULL)
					{
						if (t<=sub)	mdb_lines[line].mwnd_but[t]->SetPushed(true);
						if (t>sub)	mdb_lines[line].mwnd_but[t]->SetPushed(false);
					}
                TRACE("Recalc points: BN_CLICKED_1");
				UpdatePoints();
			}
			else
			{
				mdb_lines[line].currentlevel = 0;
				GetDocument()->SetRRNFLevel(mdb_lines[line].ra_id ,0);
				for (int t=0;t<number_of_ra_ranks;t++)
					if (mdb_lines[line].mwnd_but[t]!=NULL)
					{
						mdb_lines[line].mwnd_but[t]->SetPushed(false);
					}
                TRACE("Recalc points: BN_CLICKED_2");
				UpdatePoints();
			}

			return TRUE;
		}
		
	}
	

	return CAPage::OnCommand(wParam, lParam);
}


BOOL CPage9_RAnw::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	if (message != LVN_MOUSEMOVE)
		return CAPage::OnWndMsg(message, wParam, lParam, pResult);

	int item = LOWORD(wParam);

	if (item != -1 && mn_last_description != item && mdb_lines[item].ra_id != -1)	
	{
		mn_last_description = item;
    	gdbinterface.SelectRANF(mdb_lines[item].ra_id);
	    mwnd_description.SetWindowText(gdbinterface.GetRADescriptionNF());
	}

	return CAPage::OnWndMsg(message, wParam, lParam, pResult);
}




void CPage9_RAnw::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

	*pResult = CDRF_DODEFAULT;

	switch(lplvcd->nmcd.dwDrawStage)
	{

		case CDDS_PREPAINT:
			*pResult = CDRF_NOTIFYITEMDRAW;          // ask for item notifications.
			break;

		case CDDS_ITEMPREPAINT:
			if (mdb_lines[lplvcd->nmcd.lItemlParam].type >= 100)
			{
				lplvcd->clrTextBk = RGB(232, 232, 232);
				lplvcd->clrText = RGB(0, 0, 0);
				*pResult = CDRF_NOTIFYSUBITEMDRAW;
			}
		break;

		case CDDS_ITEMPREPAINT|CDDS_SUBITEM:
		{
			*pResult = CDRF_DODEFAULT;
			if (lplvcd->iSubItem != 4) break;

			int line = (int)lplvcd->nmcd.lItemlParam;
			if (mdb_lines[line].type < 100) break;
			if (mdb_lines[line].type > 101) break;

			// get cell coors
			CRect rc;
			GetCellRect((int)lplvcd->nmcd.dwItemSpec, 4, LVIR_BOUNDS, rc);

			// get the device context.
			CDC *pDC= CDC::FromHandle(lplvcd->nmcd.hdc);

			// clear the background
			pDC->FillSolidRect(rc,lplvcd->clrTextBk);

			int n = mdb_lines[line].count_of_levels;
			
			for (int t=0;t<n;t++)
			{
				CRect r = rc;
				r.left  += rc.Width()*t/n;
				r.right = r.left  + rc.Width()/n;

				pDC->DrawText(mdb_lines[line].levels[t].c_str(),r, DT_CENTER);
			}
			*pResult= CDRF_SKIPDEFAULT;

		}				
	}
}


void CPage9_RAnw::ItemInvalidate(int nr, BOOL erase /*= TRUE*/)
{
	CRect r;
	if (mwnd_liste.GetItemRect(nr,r,LVIR_BOUNDS) == 0) return;
	mwnd_liste.InvalidateRect(r,erase);
}


void	CPage9_RAnw::CheckPossibility(int free_points)
{
	for (int t=0;t<mdb_lines.GetCount();t++)
	if (mdb_lines[t].type != 99) // no separators
	{
		// set highlight for lower
		for (int b=0;b<mdb_lines[t].currentlevel;b++)
			if (mdb_lines[t].mwnd_but[b]!=NULL) 
				mdb_lines[t].mwnd_but[b]->SetHighlit(true);

		// possibilities
		int p=0;
		for (int b=mdb_lines[t].currentlevel;b<number_of_ra_ranks;b++)
		{
			if (mdb_lines[t].mwnd_but[b]==NULL) break;

            ASSERT(mdb_lines[t].ra_id!=-1);
			gdbinterface.SelectRANF(mdb_lines[t].ra_id);
			p += gdbinterface.GetRANFCost(b,true);

			mdb_lines[t].mwnd_but[b]->SetHighlit(p<=free_points);
		}
	}
}

//////////////////////////////////////////////////////////////////////////
void CPage9_RAnw::OnBnDoubleclickedRaReset()
{
    // set all RA's to level 0
    IntArray a_raids;
    gdbinterface.GetListOfRANF(a_raids);

    for (int t=0;t<a_raids.GetCount();t++)
    {
        GetDocument()->SetRRNFLevel((enum_RA_id)a_raids[t],0);
    }

    // total refill
    FillList();
    TRACE("Recalc points: OnBnDoubleclickedRaReset");
    UpdatePoints();
}


